import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 引入组件
import Home from '../pages/Home/Home'
import Search from '../pages/Search/Search'
import Login from '../pages/Login/Login.vue'
import Register from '../pages/Register/Register'

// 重写 push、replace 方法
// 先将原来原型方法浅拷贝一份
let Push = VueRouter.prototype.push;
let Replace = VueRouter.prototype.replace;

// 重写 push方法
VueRouter.prototype.push = function(location,resolve,reject) {
  if(resolve && reject) {
    // 篡改上下文 直接Push() this指向则是 window call()改变this指向VueRouter
    Push.call(this,location,resolve,reject)
  }else{
    Push.call(this,()=>{},()=>{})
  }
}
// 重写 replace 方法
VueRouter.prototype.replace = function(location,resolve,reject) {
  if(resolve && reject) {
    Replace.call(this,location,resolve,reject)
  }else{
    Replace.call(this,()=>{},()=>{})
  }
}

export default new VueRouter({
  routes:[
    {
      path:'/home',
      component: Home,
      meta:{show:true}
    },
    {
      path:'/',
      redirect:'/home'
    },
    {
      path:'/search',
      component: Search,
      meta:{show:true}
    },
    {
      path:'/login',
      component: Login,
      meta:{show:false}
    },
    {
      path:'/register',
      component: Register,
      meta:{show:false}
    }
  ]
})